package com.xingda.packing.dmss.minitor.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingda.packing.dmss.common.BaseController;
import com.xingda.packing.dmss.common.R;
import com.xingda.packing.dmss.common.dto.PageDto;
import com.xingda.packing.dmss.enums.WheelFinishedState;
import com.xingda.packing.dmss.enums.WheelQualityState;
import com.xingda.packing.dmss.minitor.dto.PkWheelDto;
import com.xingda.packing.dmss.minitor.entity.PkTask;
import com.xingda.packing.dmss.minitor.entity.PkWheel;
import com.xingda.packing.dmss.minitor.service.IPkTaskService;
import com.xingda.packing.dmss.minitor.service.IPkWheelService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@RestController
@RequestMapping("/api/wheel")
public class PkWheelController extends BaseController {

    @Resource
    private IPkWheelService pkWheelService;

    @Resource
    private IPkTaskService pkTaskService;

    @PostMapping("/save")
    public R savePkWheel(@RequestBody PkWheelDto dto) {
        boolean flag = this.pkWheelService.savePkWheel(dto);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("/torqueByValue")
    public R getPkWheelByValue(PageDto dto) {
        return R.ok().put("data", this.pkWheelService.getPkWheelByValue(dto));
    }

    @GetMapping("/getPage")
    public R getPkWheelPage(PageDto dto) {

        Page<PkWheelDto> page = this.pkWheelService.getPkWheelPage(dto);
        return R.ok().put("data", page);
    }

    @GetMapping("/torqueByTime")
    public R getPkWheelByTime(PageDto dto) {
        return R.ok().put("data", this.pkWheelService.getPkWheelByTime(dto));
    }


    @GetMapping("/census")
    public R getPkWheelCensus(PageDto dto) {
        // 生产总数
        int produceCount = 0;
        // 半成品
        int semiCount = 0;
        // 箱子个数
        int produceBoxCount = this.pkTaskService.lambdaQuery().eq(PkTask::getState, 3)
                .eq(PkTask::getType, 1)
                .lt(PkTask::getEndTime, dto.getEnd())
                .gt(PkTask::getEndTime, dto.getStart())
                .list().size();
        List<PkWheel> pkWheels = this.pkWheelService.lambdaQuery().ge(PkWheel::getScanTime, dto.getStart()).lt(PkWheel::getScanTime, dto.getEnd()).list();
        List<PkWheel> usedList = pkWheels.stream().filter(pkWheel -> pkWheel.getIsFinished() == WheelFinishedState.FINISHED.getCode())
                .filter(pkWheel -> pkWheel.getQualifiedState() == WheelQualityState.QUALITY.getCode()).collect(Collectors.toList());
        if (usedList.size() > 0) {
            produceCount = usedList.size();
            semiCount = pkWheels.size() - usedList.size();
        }
        JSONObject obj = new JSONObject();
        obj.put("produceBoxCount", produceBoxCount);
        obj.put("produceCount", produceCount);
        obj.put("semiCount", semiCount);
        return R.ok().put("data", obj);
    }
}

